<?php
$this->pageTitle = Yii::app()->name . ' - ' . Yii::t('home', 'profile');
$this->breadcrumbs = array(
    Yii::t('home', 'profile')
);

if (Yii::app()->user->isGuest) {
    $baseUrl = Yii::app()->getBaseUrl(true);
    $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
        'id' => 'mydialog',
        // additional javascript options for the dialog plugin
        'options' => array(
            'title' => Yii::t('home', 'message'),
            'autoOpen' => true,
            'modal' => true,
            'beforeClose' => 'js:function(){window.location = "' . $baseUrl . '/index.php?r=index";}',
            'buttons' => array(
                Yii::t('home', 'close') => 'js:function(){window.location = "' . $baseUrl . '/index.php?r=login";}'
            ),
        ),
    ));

    echo Yii::t('home', 'require_login');

    $this->endWidget('zii.widgets.jui.CJuiDialog');
    return;
}

$baseUrl = Yii::app()->request->baseUrl;
$extScript = Yii::app()->clientScript;
$extScript->registerScriptFile($baseUrl . '/js/CryptoJS-v3.1.2/rollups/md5.js');
?>
<div id="focus" class="wrapper row100 bgded" style="background-image:url('images/live-demo/background-03.jpg');">
    <div class="overlay">
        <div id="newsletter" class="clear center">
            <h2 class="uppercase font-x3 btmspace-50"><?= Yii::t('profile', 'profile') ?></h2>

            <div class="form">
                <?php
                $form = $this->beginWidget('CActiveForm', array(
                    'id' => 'profile-form',
                    'enableClientValidation' => true,
                    'clientOptions' => array(
                        'validateOnSubmit' => true,
                        'afterValidate' => "js:function(form, data, hasError) {
                if (!hasError) {
                    if(form[0][1].value) {
                        form[0][1].value = CryptoJS.MD5(form[0][1].value);
                    }
                    if(form[0][2].value) {
                        form[0][2].value = CryptoJS.MD5(form[0][2].value);
                    }
                    return true;
                }
                return false;
            }"
                    ),
                    'focus' => array($model, 'username'),
                    'htmlOptions' => array(
                        'enctype' => 'multipart/form-data'
                    )
                ));
                ?>

                <div class="row">
                    <?php echo $form->textField($model, 'username', array('value' => $customer->email, 'readonly' => true, 'placeholder' => Yii::t('register', 'email'))); ?>
                    <?php echo $form->error($model, 'username'); ?>
                </div>

                <div class="row">
                    <?php echo $form->passwordField($model, 'password', array('placeholder' => Yii::t('register', 'password'), 'autocomplete' => 'off', 'value' => '')); ?>
                    <?php echo $form->error($model, 'password'); ?>
                </div>

                <div class="row">
                    <?php echo $form->passwordField($model, 'repassword', array('placeholder' => Yii::t('register', 'repassword'), 'autocomplete' => 'off', 'value' => '')); ?>
                    <?php echo $form->error($model, 'repassword'); ?>
                </div>

                <div class="row">
                    <?php echo $form->textField($model, 'firstname', array('value' => $customer->Name, 'placeholder' => Yii::t('register', 'firstname'))); ?>
                    <?php echo $form->error($model, 'firstname'); ?>
                </div>

                <div class="row">
                    <?php echo $form->textField($model, 'lastname', array('value' => $customer->Surname, 'placeholder' => Yii::t('register', 'lastname'))); ?>
                    <?php echo $form->error($model, 'lastname'); ?>
                </div>

                <div class="row" style="display: none;">
                    <?php echo $form->textField($model, 'height', array('value' => $customer->height, 'placeholder' => Yii::t('register', 'height'))); ?>
                    <?php echo $form->error($model, 'height'); ?>
                </div>

                <div class="row" style="display: none;">
                    <?php echo $form->textField($model, 'weight', array('value' => $customer->weight, 'placeholder' => Yii::t('register', 'weight'))); ?>
                    <?php echo $form->error($model, 'weight'); ?>
                </div>

                <div class="row">
                    <?php echo $form->dropDownList($model, 'gender', array('' => Yii::t('register', 'gender'), '1' => Yii::t('register', 'male'), '2' => Yii::t('register', 'female')), array('options' => array($customer->sex => array('selected' => true)))); ?>
                    <?php echo $form->error($model, 'gender'); ?>
                </div>

                <div class="row">
                    <?php
                    $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                        'attribute' => 'birthday',
                        'model' => $model,
                        'name' => 'birthday',
                        'options' => array(
                            'defaultDate' => ConvertFormatUtil::getStringDateWithoutTime($customer->birthdate),
                            'showButtonPanel' => false,
                            'showAnim' => 'slide',
                            'dateFormat' => 'yy-mm-dd',
                            'dayNamesMin' => Yii::t('register', 'short_days'),
                            'monthNamesShort' => Yii::t('register', 'months'),
                            'changeMonth' => true,
                            'changeYear' => true,
                            'yearRange' => '1900:2099',
                            'beforeShow' => 'js:function(){  
                    if($(this).val() != ""){
                        var arrayDate = $(this).val().split("-");  
                        if (parseInt(arrayDate[0]) > 2400) {
                            arrayDate[0] = parseInt(arrayDate[0]) - 543;
                        }
                        $(this).val(arrayDate[0] + "-" + arrayDate[1] + "-" + arrayDate[2]);
                    }
                    setTimeout(function(){
                        $.each($(".ui-datepicker-year option"), function(j, k){
                            var textYear = "";
                            if(parseInt($(".ui-datepicker-year option").eq(j).val()) < 2400){
                                textYear = parseInt($(".ui-datepicker-year option").eq(j).val()) + 543;
                            }
                            $(".ui-datepicker-year option").eq(j).text(textYear);
                        });             
                    },50);
                }',
                            'afterRender' => 'js:function(){  
                    alert("sss");
                }',
                            'onClose' => 'js:function(){
                    if($(this).val() != "" && $(this).val() == dateBefore){         
                        var arrayDate = dateBefore.split("-");
                        if (parseInt(arrayDate[0]) < 2400) {
                            arrayDate[0] = parseInt(arrayDate[0]) + 543;
                        }
                        $(this).val(arrayDate[0] + "-" + arrayDate[1] + "-" + arrayDate[2]);    
                    }       
                }',
                            'onSelect' => 'js:function(dateText, inst){ 
                    dateBefore = $(this).val();
                    var arrayDate = dateText.split("-");
                    if (parseInt(arrayDate[0]) < 2400) {
                        arrayDate[0] = parseInt(arrayDate[0]) + 543;
                    }
                    $(this).val(arrayDate[0] + "-" + arrayDate[1] + "-" + arrayDate[2]);
                }',
                        ),
                        'htmlOptions' => array(
                            'value' => ConvertFormatUtil::ADStringToBEString($customer->birthdate, false),
                            'onFocus' => '$(this).removeAttr("value");',
                            'placeholder' => Yii::t('register', 'birthday')
                        ),
                    ));
                    ?>
                    <?php echo $form->error($model, 'birthday'); ?>
                </div>

                <div class="row">
                    <?php
                    $this->widget('application.extensions.timepicker.EJuiDateTimePicker', array(
                        'model' => $model,
                        'name' => 'time_of_birthday',
                        'value' => isset($customer->birthdate) ? ConvertFormatUtil::getTimeOfDate($customer->birthdate, false) : '',
                        'options' => array(
                            'showOn' => 'focus',
                            'timeFormat' => 'hh:mm',
                            'timeOnly' => true,
                        ),
                        'htmlOptions' => array(
                            'maxlength' => '10', // textField maxlength
                            'placeholder' => Yii::t('register', 'time_of_birthday')
                        ),
                    ));
                    ?>
                    <?php echo $form->error($model, 'time_of_birthday'); ?>
                </div>

                <div class="row">
                    <?php echo $form->textField($model, 'mobilephone', array('value' => $customer->telephone, 'placeholder' => Yii::t('register', 'mobilephone'))); ?>
                    <?php echo $form->error($model, 'mobilephone'); ?>
                </div>

                <?php if (isset($customer->picture) && $customer->picture != '') : ?>
                    <div class="row">
                        <?php echo CHtml::link(Yii::t('foretell', 'show'), 'javascript:void(0)', array('id' => 'show_picture', 'onclick' => '$("#picturedialog").dialog("open"); return false;')); ?>
                    </div>
                    <?php
                    $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
                        'id' => 'picturedialog',
                        // additional javascript options for the dialog plugin
                        'options' => array(
                            'title' => Yii::t('home', 'message'),
                            'autoOpen' => false,
                            'resizable' => false,
                            'modal' => true,
                            'width' => 'auto',
                            'height' => 'auto'
                        )
                    ));

                    $baseUrl = Yii::app()->getBaseUrl(true);
                    echo '<img id="image" src="' . $baseUrl . '/' . $customer->picture . '"/>';

                    $this->endWidget('zii.widgets.jui.CJuiDialog');
                endif;
                ?>

                <div class="row">
                    <?php echo $form->fileField($model, 'profile_image'); ?>
                    <?php echo $form->error($model, 'profile_image'); ?>
                </div>

                <div class="row buttons">
                    <?php echo CHtml::tag('button', array('class' => 'btn', 'type' => 'submit'), Yii::t('profile', 'update')); ?>
                    <?php echo CHtml::tag('button', array('class' => 'btn', 'type' => 'reset', 'onclick' => 'js:location.reload()'), Yii::t('profile', 'reset')); ?>
                </div>

                <?php $this->endWidget(); ?>
            </div><!-- form -->

        </div>
    </div>
</div>